/*
 * @Description: mysql
 * @Author: rendc
 * @Date: 2022-10-20 11:03:19
 * @LastEditors: rendc
 * @LastEditTime: 2022-10-20 11:28:32
 */
// 引入mysql
const mysql = require('mysql')
// console.log('🤡 CC - mysql', mysql);
// 创建连接
let connection = mysql.createConnection({
  host: "127.0.0.1",
  port: "3306",
  user: "root",
  password: "root",
  database: "test",
})
// // 开始连接 没有返回值 连接失败的话会抛出异常
// connection.connect();
// let sql = "select * from user";
// connection.query(sql, (err, result, field) => {
//   if (err) {
//     throw err;
//   }
//   console.log('🤡 CC - connection.query - result', result);
//   // console.log('🤡 CC - connection.query - field', field);
//   // 关闭连接
//   connection.end();
// })

// 根据id查找用户
function findUserById (id) {
  // 开始连接 
  connection.connect();
  let sql = "select * from user where id = " + id;
  query(sql);
}
// 根据id查询工作
function findJobById (id) {
  // 开始连接 
  connection.connect();
  let sql = "select * from job where id = " + id;
  query(sql);
}
function query (sql) {
  connection.query(sql, (err, result, field) => {
    if (err) {
      throw err;
    }
    console.log('🤡 CC - connection.query - result', result);
    // 关闭连接
    connection.end();
  })
}
findUserById(1);
// findJobById(1);
